﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;

[NetworkSettings(channel=0, sendInterval=0f)]
public class Player : NetworkBehaviour
{
    [CompilerGenerated]
    private static Func<bool> <>f__am$cache8;
    public static HashSet<Player> all = new HashSet<Player>();
    public static string customPlayerName = "Player";
    private static int kCmdCmdCreateCharacter;
    private static int kCmdCmdSetIsReady;
    private static int kCmdCmdSetName;
    private static int kCmdCmdSetSceneName;
    private static int kCmdCmdSetTeamId;
    private static int kRpcRpcSetDontDestroyOnLoad;
    [SyncVar]
    private bool syncIsReady;
    [SyncVar(hook="OnNameChanged")]
    private string syncName;
    [SyncVar]
    private string syncSceneName;
    [SyncVar]
    private int syncTeamId;

    static Player()
    {
        SceneManager.sceneLoaded += new UnityAction<Scene, LoadSceneMode>(Player.SceneLoaded);
        kCmdCmdSetSceneName = -626982698;
        NetworkBehaviour.RegisterCommandDelegate(typeof(Player), kCmdCmdSetSceneName, new NetworkBehaviour.CmdDelegate(Player.InvokeCmdCmdSetSceneName));
        kCmdCmdSetIsReady = 0x696001f8;
        NetworkBehaviour.RegisterCommandDelegate(typeof(Player), kCmdCmdSetIsReady, new NetworkBehaviour.CmdDelegate(Player.InvokeCmdCmdSetIsReady));
        kCmdCmdSetName = 0x627d38c;
        NetworkBehaviour.RegisterCommandDelegate(typeof(Player), kCmdCmdSetName, new NetworkBehaviour.CmdDelegate(Player.InvokeCmdCmdSetName));
        kCmdCmdSetTeamId = 0x25f14519;
        NetworkBehaviour.RegisterCommandDelegate(typeof(Player), kCmdCmdSetTeamId, new NetworkBehaviour.CmdDelegate(Player.InvokeCmdCmdSetTeamId));
        kCmdCmdCreateCharacter = -1619275764;
        NetworkBehaviour.RegisterCommandDelegate(typeof(Player), kCmdCmdCreateCharacter, new NetworkBehaviour.CmdDelegate(Player.InvokeCmdCmdCreateCharacter));
        kRpcRpcSetDontDestroyOnLoad = -592087709;
        NetworkBehaviour.RegisterRpcDelegate(typeof(Player), kRpcRpcSetDontDestroyOnLoad, new NetworkBehaviour.CmdDelegate(Player.InvokeRpcRpcSetDontDestroyOnLoad));
        NetworkCRC.RegisterBehaviour("Player", 0);
    }

    private void Awake()
    {
        all.Add(this);
        base.transform.parent = NetworkUtility.networkManager.transform;
        if (NetworkUtility.isServer)
        {
            this.NetworksyncSceneName = SceneManager.GetActiveScene().name;
        }
    }

    public void CallCmdCreateCharacter(Vector3 position)
    {
        if (!NetworkClient.active)
        {
            UnityEngine.Debug.LogError("Command function CmdCreateCharacter called on server.");
        }
        else if (base.isServer)
        {
            this.CmdCreateCharacter(position);
        }
        else
        {
            NetworkWriter writer = new NetworkWriter();
            writer.Write((short) 0);
            writer.Write((short) ((ushort) 5));
            writer.WritePackedUInt32((uint) kCmdCmdCreateCharacter);
            writer.Write(base.GetComponent<NetworkIdentity>().netId);
            writer.Write(position);
            base.SendCommandInternal(writer, 0, "CmdCreateCharacter");
        }
    }

    public void CallCmdSetIsReady(bool value)
    {
        if (!NetworkClient.active)
        {
            UnityEngine.Debug.LogError("Command function CmdSetIsReady called on server.");
        }
        else if (base.isServer)
        {
            this.CmdSetIsReady(value);
        }
        else
        {
            NetworkWriter writer = new NetworkWriter();
            writer.Write((short) 0);
            writer.Write((short) ((ushort) 5));
            writer.WritePackedUInt32((uint) kCmdCmdSetIsReady);
            writer.Write(base.GetComponent<NetworkIdentity>().netId);
            writer.Write(value);
            base.SendCommandInternal(writer, 0, "CmdSetIsReady");
        }
    }

    public void CallCmdSetName(string value)
    {
        if (!NetworkClient.active)
        {
            UnityEngine.Debug.LogError("Command function CmdSetName called on server.");
        }
        else if (base.isServer)
        {
            this.CmdSetName(value);
        }
        else
        {
            NetworkWriter writer = new NetworkWriter();
            writer.Write((short) 0);
            writer.Write((short) ((ushort) 5));
            writer.WritePackedUInt32((uint) kCmdCmdSetName);
            writer.Write(base.GetComponent<NetworkIdentity>().netId);
            writer.Write(value);
            base.SendCommandInternal(writer, 0, "CmdSetName");
        }
    }

    public void CallCmdSetSceneName(string value)
    {
        if (!NetworkClient.active)
        {
            UnityEngine.Debug.LogError("Command function CmdSetSceneName called on server.");
        }
        else if (base.isServer)
        {
            this.CmdSetSceneName(value);
        }
        else
        {
            NetworkWriter writer = new NetworkWriter();
            writer.Write((short) 0);
            writer.Write((short) ((ushort) 5));
            writer.WritePackedUInt32((uint) kCmdCmdSetSceneName);
            writer.Write(base.GetComponent<NetworkIdentity>().netId);
            writer.Write(value);
            base.SendCommandInternal(writer, 0, "CmdSetSceneName");
        }
    }

    public void CallCmdSetTeamId(int value)
    {
        if (!NetworkClient.active)
        {
            UnityEngine.Debug.LogError("Command function CmdSetTeamId called on server.");
        }
        else if (base.isServer)
        {
            this.CmdSetTeamId(value);
        }
        else
        {
            NetworkWriter writer = new NetworkWriter();
            writer.Write((short) 0);
            writer.Write((short) ((ushort) 5));
            writer.WritePackedUInt32((uint) kCmdCmdSetTeamId);
            writer.Write(base.GetComponent<NetworkIdentity>().netId);
            writer.WritePackedUInt32((uint) value);
            base.SendCommandInternal(writer, 0, "CmdSetTeamId");
        }
    }

    public void CallRpcSetDontDestroyOnLoad(bool value)
    {
        if (!NetworkServer.active)
        {
            UnityEngine.Debug.LogError("RPC Function RpcSetDontDestroyOnLoad called on client.");
        }
        else
        {
            NetworkWriter writer = new NetworkWriter();
            writer.Write((short) 0);
            writer.Write((short) ((ushort) 2));
            writer.WritePackedUInt32((uint) kRpcRpcSetDontDestroyOnLoad);
            writer.Write(base.GetComponent<NetworkIdentity>().netId);
            writer.Write(value);
            this.SendRPCInternal(writer, 0, "RpcSetDontDestroyOnLoad");
        }
    }

    [Command]
    private void CmdCreateCharacter(Vector3 position)
    {
        if (this.character == null)
        {
            PlayerCharacter character = GameResources.PlayerCharacter.CloneGameObject<PlayerCharacter>();
            character.SetPosition(position);
            character.NetworksyncPlayerId = base.netId;
            NetworkServer.AddPlayerForConnection(base.connectionToClient, character.gameObject, 1);
        }
    }

    [Command]
    private void CmdSetIsReady(bool value)
    {
        this.NetworksyncIsReady = value;
    }

    [Command]
    private void CmdSetName(string value)
    {
        this.NetworksyncName = value;
    }

    [Command]
    private void CmdSetSceneName(string value)
    {
        this.NetworksyncSceneName = value;
    }

    [Command]
    private void CmdSetTeamId(int value)
    {
        this.NetworksyncTeamId = value;
    }

    public static CustomYieldInstruction Create()
    {
        if (local != null)
        {
            return null;
        }
        ClientScene.AddPlayer(0);
        if (<>f__am$cache8 == null)
        {
            <>f__am$cache8 = () => local != null;
        }
        return new WaitUntil(<>f__am$cache8);
    }

    public WaitUntil CreateCharacter(Vector3 position)
    {
        if (this.character != null)
        {
            return null;
        }
        if (ClientScene.localPlayers.Count == 1)
        {
            ClientScene.localPlayers.Add(null);
        }
        this.CallCmdCreateCharacter(position);
        return new WaitUntil(() => this.character != null);
    }

    public override float GetNetworkSendInterval()
    {
        return 0f;
    }

    protected static void InvokeCmdCmdCreateCharacter(NetworkBehaviour obj, NetworkReader reader)
    {
        if (!NetworkServer.active)
        {
            UnityEngine.Debug.LogError("Command CmdCreateCharacter called on client.");
        }
        else
        {
            ((Player) obj).CmdCreateCharacter(reader.ReadVector3());
        }
    }

    protected static void InvokeCmdCmdSetIsReady(NetworkBehaviour obj, NetworkReader reader)
    {
        if (!NetworkServer.active)
        {
            UnityEngine.Debug.LogError("Command CmdSetIsReady called on client.");
        }
        else
        {
            ((Player) obj).CmdSetIsReady(reader.ReadBoolean());
        }
    }

    protected static void InvokeCmdCmdSetName(NetworkBehaviour obj, NetworkReader reader)
    {
        if (!NetworkServer.active)
        {
            UnityEngine.Debug.LogError("Command CmdSetName called on client.");
        }
        else
        {
            ((Player) obj).CmdSetName(reader.ReadString());
        }
    }

    protected static void InvokeCmdCmdSetSceneName(NetworkBehaviour obj, NetworkReader reader)
    {
        if (!NetworkServer.active)
        {
            UnityEngine.Debug.LogError("Command CmdSetSceneName called on client.");
        }
        else
        {
            ((Player) obj).CmdSetSceneName(reader.ReadString());
        }
    }

    protected static void InvokeCmdCmdSetTeamId(NetworkBehaviour obj, NetworkReader reader)
    {
        if (!NetworkServer.active)
        {
            UnityEngine.Debug.LogError("Command CmdSetTeamId called on client.");
        }
        else
        {
            ((Player) obj).CmdSetTeamId((int) reader.ReadPackedUInt32());
        }
    }

    protected static void InvokeRpcRpcSetDontDestroyOnLoad(NetworkBehaviour obj, NetworkReader reader)
    {
        if (!NetworkClient.active)
        {
            UnityEngine.Debug.LogError("RPC RpcSetDontDestroyOnLoad called on server.");
        }
        else
        {
            ((Player) obj).RpcSetDontDestroyOnLoad(reader.ReadBoolean());
        }
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        if (initialState)
        {
            this.syncSceneName = reader.ReadString();
            this.syncIsReady = reader.ReadBoolean();
            this.syncName = reader.ReadString();
            this.syncTeamId = (int) reader.ReadPackedUInt32();
        }
        else
        {
            int num = (int) reader.ReadPackedUInt32();
            if ((num & 1) != 0)
            {
                this.syncSceneName = reader.ReadString();
            }
            if ((num & 2) != 0)
            {
                this.syncIsReady = reader.ReadBoolean();
            }
            if ((num & 4) != 0)
            {
                this.OnNameChanged(reader.ReadString());
            }
            if ((num & 8) != 0)
            {
                this.syncTeamId = (int) reader.ReadPackedUInt32();
            }
        }
    }

    private void OnDestroy()
    {
        if (local == this)
        {
            local = null;
        }
        all.Remove(this);
    }

    private void OnNameChanged(string value)
    {
        base.name = value;
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        if (forceAll)
        {
            writer.Write(this.syncSceneName);
            writer.Write(this.syncIsReady);
            writer.Write(this.syncName);
            writer.WritePackedUInt32((uint) this.syncTeamId);
            return true;
        }
        bool flag = false;
        if ((base.syncVarDirtyBits & 1) != 0)
        {
            if (!flag)
            {
                writer.WritePackedUInt32(base.syncVarDirtyBits);
                flag = true;
            }
            writer.Write(this.syncSceneName);
        }
        if ((base.syncVarDirtyBits & 2) != 0)
        {
            if (!flag)
            {
                writer.WritePackedUInt32(base.syncVarDirtyBits);
                flag = true;
            }
            writer.Write(this.syncIsReady);
        }
        if ((base.syncVarDirtyBits & 4) != 0)
        {
            if (!flag)
            {
                writer.WritePackedUInt32(base.syncVarDirtyBits);
                flag = true;
            }
            writer.Write(this.syncName);
        }
        if ((base.syncVarDirtyBits & 8) != 0)
        {
            if (!flag)
            {
                writer.WritePackedUInt32(base.syncVarDirtyBits);
                flag = true;
            }
            writer.WritePackedUInt32((uint) this.syncTeamId);
        }
        if (!flag)
        {
            writer.WritePackedUInt32(base.syncVarDirtyBits);
        }
        return flag;
    }

    public override void OnStartAuthority()
    {
        base.OnStartAuthority();
        local = this;
        this.SetName(customPlayerName);
    }

    public override void OnStartClient()
    {
        base.name = this.syncName;
    }

    [ClientRpc]
    private void RpcSetDontDestroyOnLoad(bool value)
    {
        <RpcSetDontDestroyOnLoad>c__AnonStorey61 storey = new <RpcSetDontDestroyOnLoad>c__AnonStorey61 {
            value = value
        };
        PlayerCharacter.all.ForEach(new Action<PlayerCharacter>(storey.<>m__BD));
        if (storey.value)
        {
            Time.timeScale = 0f;
            local.SetIsReady(true);
        }
    }

    private static void SceneLoaded(Scene scene, LoadSceneMode mode)
    {
        if (local != null)
        {
            local.CallCmdSetSceneName(scene.name);
        }
    }

    public WaitUntil SetIsReady(bool value)
    {
        <SetIsReady>c__AnonStorey5E storeye = new <SetIsReady>c__AnonStorey5E {
            value = value,
            <>f__this = this
        };
        this.CallCmdSetIsReady(storeye.value);
        return new WaitUntil(new Func<bool>(storeye.<>m__B9));
    }

    public WaitUntil SetName(string value)
    {
        <SetName>c__AnonStorey5F storeyf = new <SetName>c__AnonStorey5F {
            value = value,
            <>f__this = this
        };
        this.CallCmdSetName(storeyf.value);
        return new WaitUntil(new Func<bool>(storeyf.<>m__BA));
    }

    public WaitUntil SetTeamId(int value)
    {
        <SetTeamId>c__AnonStorey60 storey = new <SetTeamId>c__AnonStorey60 {
            value = value,
            <>f__this = this
        };
        this.CallCmdSetTeamId(storey.value);
        return new WaitUntil(new Func<bool>(storey.<>m__BB));
    }

    private void UNetVersion()
    {
    }

    [Server]
    internal Coroutine Warp(string levelName)
    {
        if (!NetworkServer.active)
        {
            UnityEngine.Debug.LogWarning("[Server] function 'UnityEngine.Coroutine Player::Warp(System.String)' called on client");
            return new Coroutine();
        }
        return base.StartCoroutine(this.WarpRoutine(levelName));
    }

    [DebuggerHidden]
    private IEnumerator WarpRoutine(string levelName)
    {
        return new <WarpRoutine>c__IteratorC { levelName = levelName, <$>levelName = levelName, <>f__this = this };
    }

    public PlayerCharacter character { get; internal set; }

    public bool isReady
    {
        get
        {
            return this.syncIsReady;
        }
    }

    public static Player local
    {
        [CompilerGenerated]
        get
        {
            return <local>k__BackingField;
        }
        [CompilerGenerated]
        private set
        {
            <local>k__BackingField = value;
        }
    }

    public bool NetworksyncIsReady
    {
        get
        {
            return this.syncIsReady;
        }
        [param: In]
        set
        {
            base.SetSyncVar<bool>(value, ref this.syncIsReady, 2);
        }
    }

    public string NetworksyncName
    {
        get
        {
            return this.syncName;
        }
        [param: In]
        set
        {
            if (NetworkServer.localClientActive && !base.syncVarHookGuard)
            {
                base.syncVarHookGuard = true;
                this.OnNameChanged(value);
                base.syncVarHookGuard = false;
            }
            base.SetSyncVar<string>(value, ref this.syncName, 4);
        }
    }

    public string NetworksyncSceneName
    {
        get
        {
            return this.syncSceneName;
        }
        [param: In]
        set
        {
            base.SetSyncVar<string>(value, ref this.syncSceneName, 1);
        }
    }

    public int NetworksyncTeamId
    {
        get
        {
            return this.syncTeamId;
        }
        [param: In]
        set
        {
            base.SetSyncVar<int>(value, ref this.syncTeamId, 8);
        }
    }

    public string sceneName
    {
        get
        {
            return this.syncSceneName;
        }
        private set
        {
            this.NetworksyncSceneName = value;
        }
    }

    public int teamId
    {
        get
        {
            return this.syncTeamId;
        }
    }

    [CompilerGenerated]
    private sealed class <RpcSetDontDestroyOnLoad>c__AnonStorey61
    {
        internal bool value;

        internal void <>m__BD(PlayerCharacter v)
        {
            v.SetDontDestroyOnLoad(this.value);
        }
    }

    [CompilerGenerated]
    private sealed class <SetIsReady>c__AnonStorey5E
    {
        internal Player <>f__this;
        internal bool value;

        internal bool <>m__B9()
        {
            return (this.<>f__this.syncIsReady == this.value);
        }
    }

    [CompilerGenerated]
    private sealed class <SetName>c__AnonStorey5F
    {
        internal Player <>f__this;
        internal string value;

        internal bool <>m__BA()
        {
            return (this.<>f__this.syncName == this.value);
        }
    }

    [CompilerGenerated]
    private sealed class <SetTeamId>c__AnonStorey60
    {
        internal Player <>f__this;
        internal int value;

        internal bool <>m__BB()
        {
            return (this.<>f__this.syncTeamId == this.value);
        }
    }

    [CompilerGenerated]
    private sealed class <WarpRoutine>c__IteratorC : IDisposable, IEnumerator, IEnumerator<object>
    {
        internal object $current;
        internal int $PC;
        internal string <$>levelName;
        private static Action<Player> <>f__am$cache5;
        private static Func<bool> <>f__am$cache6;
        private static Func<Player, bool> <>f__am$cache7;
        internal Player <>f__this;
        internal string levelName;

        private static void <>m__BF(Player x)
        {
            x.SetIsReady(false);
        }

        private static bool <>m__C0()
        {
            if (<>f__am$cache7 == null)
            {
                <>f__am$cache7 = x => !x.isReady;
            }
            return (Player.all.Count<Player>(<>f__am$cache7) == 0);
        }

        private static bool <>m__C1(Player x)
        {
            return !x.isReady;
        }

        [DebuggerHidden]
        public void Dispose()
        {
            this.$PC = -1;
        }

        public bool MoveNext()
        {
            uint num = (uint) this.$PC;
            this.$PC = -1;
            switch (num)
            {
                case 0:
                    if (<>f__am$cache5 == null)
                    {
                        <>f__am$cache5 = new Action<Player>(Player.<WarpRoutine>c__IteratorC.<>m__BF);
                    }
                    Player.all.Foreach<Player>(<>f__am$cache5);
                    this.<>f__this.CallRpcSetDontDestroyOnLoad(true);
                    if (<>f__am$cache6 == null)
                    {
                        <>f__am$cache6 = new Func<bool>(Player.<WarpRoutine>c__IteratorC.<>m__C0);
                    }
                    this.$current = new WaitUntil(<>f__am$cache6);
                    this.$PC = 1;
                    goto Label_00BE;

                case 1:
                    this.$current = IngameScene.Load(this.levelName);
                    this.$PC = 2;
                    goto Label_00BE;

                case 2:
                    this.<>f__this.CallRpcSetDontDestroyOnLoad(false);
                    this.$PC = -1;
                    break;
            }
            return false;
        Label_00BE:
            return true;
        }

        [DebuggerHidden]
        public void Reset()
        {
            throw new NotSupportedException();
        }

        object IEnumerator<object>.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }

        object IEnumerator.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }
    }
}

